package com.css.zfzx.sjcj.modules.boundary.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.boundary.repository.BoundaryNativeRepository;
import com.css.zfzx.sjcj.modules.boundary.repository.entity.BoundaryEntity;
import com.css.zfzx.sjcj.modules.boundary.service.BoundaryService;
import com.css.zfzx.sjcj.modules.boundary.viewobjects.BoundaryQueryParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author andy
 * @date 2021-01-26
 */
@Service
@Slf4j
public class BoundaryServiceImpl implements BoundaryService {

    @Autowired
    private BoundaryNativeRepository boundaryNativeRepository;

    @Override
    public JSONObject getBoundaries(BoundaryQueryParams queryParams) {
        JSONObject jsonObject = new JSONObject();
        List<BoundaryEntity> boundaryEntities = this.boundaryNativeRepository.listBoundaries(queryParams);
        for (BoundaryEntity boundaryEntity : boundaryEntities) {
            String code = boundaryEntity.getCode();
            if (code.equals("110100")) {
                boundaryEntity.setName("北京市");
            } else if (code.equals("500100")) {
                boundaryEntity.setName("重庆市");
            }
            String coords = boundaryEntity.getCoordinate();
            if (coords == null) continue;
            coords = coords.substring(coords.indexOf("(((") + 3, coords.lastIndexOf(")))"));
            coords = coords.replace(")), ((", "|");
            coords = coords.replace("), (", "#");
            coords = coords.replace(", ", ";");
            coords = coords.replace(" ", ",");
            jsonObject.put(boundaryEntity.getName(), coords);
        }
        return jsonObject;
    }

    @Override
    public List<String> getDistrictCode(BoundaryQueryParams queryParams) {
        List<String> result = new ArrayList<>();
        List<BoundaryEntity> boundaryEntities = this.boundaryNativeRepository.listBoundaries(queryParams);
        for (BoundaryEntity boundaryEntity : boundaryEntities) {
            String code = boundaryEntity.getCode(), name = boundaryEntity.getName();
            result.add(code + "_" + name);
        }
        return result;
    }

    @Override
    public List<String> getDistrictNumber(BoundaryQueryParams queryParams) {
        List<String> result = new ArrayList<>();
        List<BoundaryEntity> boundaryEntities = this.boundaryNativeRepository.listBoundaries(queryParams);
        for (BoundaryEntity boundaryEntity : boundaryEntities) {
            String level = boundaryEntity.getLevel(), code = boundaryEntity.getCode(), name = boundaryEntity.getName();
            if ("0".equals(level)) {
                code = code.substring(0, 2);
            } else if ("1".equals(level)) {
                code = code.substring(0, 4);
            }
            result.add(code + "_" + name);
        }
        return result;
    }

    @Override
    public Map<String, String> listAreaCode2NameByProvinceAndCity(String province, String city, String area, String type) {
        Map<String, String> code2name = new HashMap<>();
        List<BoundaryEntity> boundaryEntities = this.boundaryNativeRepository.listAreasBoundaryByProvinceAndCity(province, city, area, type);
        for (BoundaryEntity boundaryEntity : boundaryEntities) {
            String code = boundaryEntity.getCode();
            if (code.equals("110100")) {
                boundaryEntity.setName("北京市");
            } else if (code.equals("500100")) {
                boundaryEntity.setName("重庆市");
            }
            code2name.put(boundaryEntity.getCode(), boundaryEntity.getName());
        }
        return code2name;
    }
}
